<!--
 * @Descripttion: 
 * @Author: wang pingqi
 * @Date: 2022-04-02 10:56:46
 * @LastEditors: wang pingqi
 * @LastEditTime: 2022-04-28 16:51:04
-->
<template>
  <AuthButton
    v-if="authBtn"
    @click="exportExcel"
    :loading="exportListLoading"
    v-bind="attribute"
    :disabled="disabled"
  />
  <el-button
    v-else
    @click="exportExcel"
    :loading="exportListLoading"
    v-bind="attribute"
  >
    {{ name }}
  </el-button>
</template>
<script>
import 'sweetalert2-preset/dist/style.css'
import { error, confirm } from 'sweetalert2-preset'

export default {
  name: 'ExportExcel',
  props: {
    // 是否是权限按钮
    authBtn: {
      type: Boolean,
      default: true
    },
    url: {
      require: true,
      type: String
    },
    param: {
      require: false,
      type: Object,
      default () {
        return {}
      }
    },
    name: {
      default: '导出'
    },
    disabled: {
      require: false,
      default: false
    }
  },
  data () {
    return {
      exportListLoading: false
    }
  },
  computed: {
    attribute () {
      return {
        name: this.name,
        icon: 'el-icon-download',
        ...this.$attrs
      }
    }
  },
  methods: {
    exportExcel () {
      if (!this.url) {
        error('url不能为空')
        return
      }
      this.exportListLoading = true
      this.$POST(this.url, this.param)
        .then(res => {
          if (res.errorCode == '00000') {
            confirm({
              title: '导出成功',
              html: `导出文件已保存到 <b> 系统管理 - 文档下载 </b> 中，请前往手动下载。`,
              icon: 'info',
              confirmButtonText: '立即前往',
              cancelButtonText: '我知道了'
            }).then(_ => {
              this.$router.push('/system/export-file')
            })
          } else {
            error(res.message)
          }
        })
        .finally(_ => {
          this.exportListLoading = false
        })
    }
  }
}
</script>
